<template>
  <div class="app-container">
    <!--   搜索  -->
    <search-form
      v-show="showSearch"
      :data="queryParams"
      :field-list="filterInfo.fieldList"
      :list-type-info="filterInfo.dictList"
      @handleSearch="handleQuery"
      @handleReset="resetQuery" />
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" icon="el-icon-plus" size="mini" @click="handleAdd" v-hasPermi="['system:menu:add']"
          >新增
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button icon="el-icon-sort" size="mini" @click="toggleExpandAll">展开/折叠 </el-button>
      </el-col>
      <!--      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>-->
    </el-row>
    <hk-table
      v-if="refreshTable"
      :loading="loading"
      :tableData="menuList"
      :tableColumns="tableFieldList"
      :default-expand-all="isExpandAll"
      :showPag="false"
      :operate="true"
      rowkey="id"
      :table-height="80"
      :tree-props="{ children: 'children', hasChildren: 'hasChildren' }">
      <!--   图标   -->
      <template #icon="scope">
        <svg-icon :icon-class="scope.row.icon" />
      </template>
      <!--   状态   -->
      <template #status="scope">
        <span class="Inspan" :class="scope.row.status == 0 ? 'simple' : 'serious'">{{
          scope.row.status == 0 ? '正常' : scope.row.status == -1 ? '删除' : '停用'
        }}</span>
      </template>
      <!--   操作   -->
      <template #operate="scope">
        <el-button size="mini" type="text" @click="handleUpdate(scope.row)" v-hasPermi="['system:menu:edit']"
          >修改
        </el-button>
        <el-button size="mini" type="text" @click="handleAdd(scope.row)" v-hasPermi="['system:menu:add']"
          >新增
        </el-button>
        <el-button size="mini" type="text" @click="handleDelete(scope.row)" v-hasPermi="['system:menu:remove']"
          >删除
        </el-button>
      </template>
    </hk-table>

    <!-- 添加或修改菜单对话框 -->
    <edit-dialog
      ref="editDialogRef"
      :title="title"
      :form-data="form"
      :visibleStatus="dict.type.visible_status"
      :menuOptions="menuOptions"
      :menus-status="filterInfo.dictList.statusList"
      @submitForm="submitForm" />
  </div>
</template>

<script>
import { listMenu, getMenu, delMenu } from '@/api/system/menu'

import IconSelect from '@/components/IconSelect'
import SearchForm from '@/components/HKSearchform/index.vue'
import HkTable from '@/components/HKTable'
import editDialog from './form'

export default {
  name: 'Menu',
  dicts: ['visible_status', 'sys_normal_disable', 'menu_status'],
  components: { IconSelect, SearchForm, HkTable, editDialog },
  data() {
    return {
      // 搜索框
      filterInfo: {
        fieldList: [
          { label: '菜单名称', type: 'input', field: 'name' },
          { label: '状态', type: 'select', field: 'status', list: 'statusList' },
        ],
        dictList: {
          statusList: [],
        },
      },
      // 表格配置项
      tableFieldList: [
        { label: '菜单名称', param: 'name' },
        { label: '图标', param: 'icon', type: 'slot' },
        { label: '排序', param: 'orderNum' },
        { label: '权限标识', param: 'permission' },
        { label: '组件路径', param: 'path' },
        { label: '状态', param: 'status', type: 'slot' },
        { label: '创建时间', param: 'createTime' },
      ],
      // 遮罩层
      loading: true,
      // 显示搜索条件
      showSearch: true,
      // 菜单表格树数据
      menuList: [],
      // 菜单树选项
      menuOptions: [],
      // 弹出层标题
      title: '',

      // 是否展开，默认全部折叠
      isExpandAll: false,
      // 重新渲染表格状态
      refreshTable: true,
      // 查询参数
      queryParams: {
        name: undefined,
        status: undefined,
      },
      // 表单参数
      form: {},
    }
  },
  created() {
    this.getList()
  },
  methods: {
    onDictReady(dict) {
      this.filterInfo.dictList.statusList = dict.type.menu_status
    },
    /** 查询菜单列表 */
    getList() {
      this.loading = true

      listMenu(this.queryParams).then((response) => {
        this.menuList = this.handleTree(response, 'id')
        this.loading = false
      })
    },
    /** 查询菜单下拉树结构 */
    getTreeselect() {
      listMenu().then((response) => {
        this.menuOptions = []
        const menu = { id: 0, name: '主类目', children: [] }
        menu.children = this.handleTree(response, 'id')
        this.menuOptions.push(menu)
      })
    },
    /** 搜索按钮操作 */
    handleQuery(data) {
      if (data) {
        this.queryParams = { ...data }
      } else {
        this.queryParams = {
          name: undefined,
          status: undefined,
        }
      }
    this.queryParams.current = 1
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm('queryForm')
      this.handleQuery()
    },
    /** 新增按钮操作 */
    handleAdd(row) {
      this.getTreeselect()
      this.form = {
        id: undefined,
        parentId: 0,
        menuName: undefined,
        icon: undefined,
        menuType: 'M',
        orderNum: undefined,
        isFrame: 1,
        isCache: '0',
        isInline: '1',
        visible: '0',
        status: '0',
      }
      if (row != null && row.id) {
        this.form.parentId = row.id
      } else {
        this.form.parentId = 0
      }
      this.$refs.editDialogRef.show()
      this.title = '添加菜单'
    },
    /** 展开/折叠操作 */
    toggleExpandAll() {
      this.refreshTable = false
      this.isExpandAll = !this.isExpandAll
      this.$nextTick(() => {
        this.refreshTable = true
      })
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.getTreeselect()
      getMenu(row.id).then((response) => {
        this.form = response
        this.$refs.editDialogRef.show()
        this.title = '修改菜单'
      })
    },
    /** 保存按钮操作 */
    submitForm() {
      this.getList()
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      this.$modal
        .confirm('是否确认删除名称为"' + row.name + '"的数据项？')
        .then(function () {
          return delMenu(row.id)
        })
        .then(() => {
          this.getList()
          this.$modal.msgSuccess('删除成功')
        })
        .catch(() => {})
    },
  },
}
</script>
<style lang="scss" scoped>
::v-deep  .targetTable{
    flex: 1 !important;
  }

.app-container{
  height: 100% !important;
  width: 100%;
  display: flex;
  flex-direction: column;
  overflow: hidden;
}</style>